package com.longcheer.weibo;

import com.longcheer.weibo.util.WeiboConfig;
import com.weibo.net.AccessToken;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;

public class MainActivity extends TabActivity {

    private static final String TAG = "MainActivity";
    private Resources mRes;
    
    private AlertDialog.Builder mDialogBuilder;
    
    public MainActivity() {
        
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if (getIntent().getExtras() == null || getIntent().getBooleanExtra("authed", false) == false) {
            Uri uri = getIntent().getData();
            String oauthVerifier = uri.getQueryParameter("oauth_verifier");
            Weibo.getInstance().addOauthverifier(oauthVerifier);
            try {
                AccessToken accessToken = Weibo.getInstance().generateAccessToken(this, null);
                WeiboConfig.putAccessToken(this, accessToken);
                Weibo.getInstance().setAccessToken(WeiboConfig.accessToken);
            } catch (WeiboException e) {
                Log.e(TAG, "Generate AccessToken error.", e);
                return;
            }
        }
        
        setContentView(R.layout.main);

        mRes = getResources();
        
        TabHost tabs = getTabHost();
        TabHost.TabSpec spec = tabs.newTabSpec("home");
        spec.setIndicator(mRes.getText(R.string.tab_home), mRes.getDrawable(R.drawable.icon_home));
        spec.setContent(new Intent().setClass(this, HomeActivity.class));
        tabs.addTab(spec);
        
        spec = tabs.newTabSpec("message");
        spec.setIndicator(mRes.getText(R.string.tab_message), mRes.getDrawable(R.drawable.icon_message));
        spec.setContent(new Intent().setClass(this, MessageActivity.class));
        tabs.addTab(spec);
        
        spec = tabs.newTabSpec("profile");
        spec.setIndicator(mRes.getText(R.string.tab_profile), mRes.getDrawable(R.drawable.icon_profile));
        Intent intent = new Intent().setClass(this, ProfileActivity.class);
        intent.putExtra("user", LoginActivity.me);
        intent.putExtra("main", true);
        spec.setContent(intent);
        tabs.addTab(spec);
        
        spec = tabs.newTabSpec("square");
        spec.setIndicator(mRes.getText(R.string.tab_square), mRes.getDrawable(R.drawable.icon_square));
        spec.setContent(new Intent().setClass(this, SquareActivity.class));
        tabs.addTab(spec);
        
        spec = tabs.newTabSpec("more");
        spec.setIndicator(mRes.getText(R.string.tab_more), mRes.getDrawable(R.drawable.icon_more));
        spec.setContent(new Intent().setClass(this, MoreInfoActivity.class));
        tabs.addTab(spec);
        
        tabs.setCurrentTab(0);
    }

    @Override
    public void onBackPressed() {
        mDialogBuilder = new AlertDialog.Builder(this);
        mDialogBuilder.setTitle(R.string.exit_confirm_title)
            .setMessage(R.string.exit_confirm_msg)
            .setPositiveButton(R.string.exit_weibo, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MainActivity.this.finish();
                }
            })
            .setCancelable(true)
            .create()
            .show();
    }
    
    
}